#ifndef ldef_h
#define ldef_h

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);

#ifndef luaL_checkversion

#define luaL_checkversion(L)

#define lua_rawlen lua_objlen

#define luaL_newlibtable(L,l) \
  lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)

#define luaL_newlib(L,l)  (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))

void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup)
{
    luaL_checkversion(L);
    luaL_checkstack(L, nup, "too many upvalues");
    for (; l->name != NULL; l++) {  /* fill the table with given functions */
        int i;
        for (i = 0; i < nup; i++)  /* copy upvalues to the top */
            lua_pushvalue(L, -nup);
        lua_pushcclosure(L, l->func, nup);  /* closure with those upvalues */
        lua_setfield(L, -(nup + 2), l->name);
    }
    lua_pop(L, nup);  /* remove upvalues */
}

#endif

#endif

